package com.lsl.learn.springcloud.controller;

import com.lsl.learn.springcloud.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * 消费者不应该有service层，因为消费者不应该关心这些
 *
 * @ClassName DeptConsumerController
 * @Description TODO
 * @Author LSL
 * @Date 2019/8/17 13:22
 * @Version 1.0
 * @Email lsl.yx@foxmail.com
 **/
@RestController
public class DeptConsumerController {

    // 单机写死
//    private static final String REST_URL_PREFIX = "http://localhost:8001";

    // 微服务名称，通过微服务名称访问，不用再关心地址和端口号
    private static final String REST_URL_PREFIX = "http://MICROSERVICECLOUD-DEPT";

    // RestTemplate 提供了多种便捷访问远程HTTP服务的方法，客户端模板工具集
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/consumer/dept/addition")
    public boolean addition(@RequestBody Dept dept) {
        return restTemplate.postForObject(REST_URL_PREFIX + "/dept/addition", dept, Boolean.class);
    }

    @RequestMapping(value = "/consumer/dept/number/{id}")
    public Dept number(@PathVariable("id") Long id) {
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/number/" + id, Dept.class);
    }

    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/consumer/dept/all")
    public List<Dept> all() {
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/all", List.class);
    }

    @RequestMapping(value = "/consumer/discovery")
    public Object discovery() {
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/discovery", Object.class);
    }
}
